home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 15 / Amiga Plus Leser CD 15.iso / Tools / Development / yacas_alg / yacas_morphos / share / yacas / include / infixparser.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-13  |  3.5 KB  |  137 lines

  1. /** \file infixparser.h
  2.  *  parsing and printing in the infix style.
  3.  *
  4.  */
  5.  
  6.  
  7. #ifndef __infixparser_h__
  8. #define __infixparser_h__
  9.  
  10. #include "yacasbase.h"
  11. #include "lispparser.h"
  12.  
  13.  
  14. #define KMaxPrecedence 60000
  15.  
  16. class LispInFixOperator : public YacasBase
  17. {
  18. public:
  19.     inline LispInFixOperator(LispInt aPrecedence)
  20.         : iPrecedence(aPrecedence),
  21.         iLeftPrecedence(aPrecedence),
  22.         iRightPrecedence(aPrecedence),
  23.         iRightAssociative(0)
  24.     {};
  25.     inline void SetRightAssociative(void)
  26.     {
  27.         iRightAssociative = 1;
  28.     }
  29.     inline void SetLeftPrecedence(LispInt aPrecedence)
  30.     {
  31.         iLeftPrecedence = aPrecedence;
  32.     }
  33.     inline void SetRightPrecedence(LispInt aPrecedence)
  34.     {
  35.         iRightPrecedence = aPrecedence;
  36.     }
  37. public:
  38.     LispInt iPrecedence;
  39.     LispInt iLeftPrecedence;
  40.     LispInt iRightPrecedence;
  41.     LispInt iRightAssociative;
  42. };
  43.  
  44. class LispOperators : public LispAssociatedHash<LispInFixOperator>
  45. {
  46. public:
  47.     void SetOperator(LispInt aPrecedence,LispStringPtr aString);
  48.     void SetRightAssociative(LispStringPtr aString);
  49.     void SetLeftPrecedence(LispStringPtr aString,LispInt aPrecedence);
  50.     void SetRightPrecedence(LispStringPtr aString,LispInt aPrecedence);
  51. };
  52.  
  53. class InfixParser : public LispParser
  54. {
  55. public:
  56.     InfixParser(LispTokenizer& aTokenizer, LispInput& aInput,
  57.                 LispHashTable& aHashTable,
  58.                 LispOperators& aPrefixOperators,
  59.                 LispOperators& aInfixOperators,
  60.                 LispOperators& aPostfixOperators,
  61.                 LispOperators& aBodiedOperators);
  62.     ~InfixParser();
  63.     
  64.     virtual void Parse(LispPtr& aResult, LispEnvironment& aEnvironment );
  65. //private:
  66.     void Parse(LispPtr& aResult);
  67. public:
  68.     LispOperators& iPrefixOperators;
  69.     LispOperators& iInfixOperators;
  70.     LispOperators& iPostfixOperators;
  71.     LispOperators& iBodiedOperators;
  72.  
  73.     LispEnvironment* iEnvironment;
  74. };
  75.  
  76. class ParsedObject : public YacasBase
  77. {
  78. public:
  79.     ParsedObject(InfixParser& aParser)
  80.         : iParser(aParser),
  81.           iError(LispFalse),
  82.           iEndOfFile(LispFalse),
  83.           iLookAhead(NULL)  {};
  84.     void Parse();
  85. private:
  86.     void ReadToken();
  87.     void MatchToken(LispStringPtr aToken);
  88.     void ReadExpression(LispInt depth);
  89.     void ReadAtom();
  90. private:
  91.     void GetOtherSide(LispInt aNrArgsToCombine, LispInt depth);
  92.     void Combine(LispInt aNrArgsToCombine);
  93.     void InsertAtom(LispStringPtr aString);
  94. private:
  95.     InfixParser& iParser;
  96. private:
  97.     LispBoolean iError;
  98.     LispBoolean iEndOfFile;
  99.     LispStringPtr iLookAhead;
  100. public:
  101.     LispPtr iResult;
  102. };
  103.  
  104.  
  105. class InfixPrinter : public LispPrinter
  106. {
  107. public:
  108.     InfixPrinter(LispOperators& aPrefixOperators,
  109.                  LispOperators& aInfixOperators,
  110.                  LispOperators& aPostfixOperators,
  111.                  LispOperators& aBodiedOperators)
  112.         : iPrefixOperators(aPrefixOperators),
  113.           iInfixOperators(aInfixOperators),
  114.           iPostfixOperators(aPostfixOperators),
  115.           iBodiedOperators(aBodiedOperators),
  116.           iPrevLastChar(0){}
  117.  
  118.     virtual void Print(LispPtr& aExpression, LispOutput& aOutput,
  119.                        LispEnvironment& aEnvironment);
  120. private:
  121.     void Print(LispPtr& aExpression, LispOutput& aOutput,
  122.                LispInt iPrecedence);
  123.     void WriteToken(LispOutput& aOutput,LispCharPtr aString);
  124. private:
  125.     
  126.     LispOperators& iPrefixOperators;
  127.     LispOperators& iInfixOperators;
  128.     LispOperators& iPostfixOperators;
  129.     LispOperators& iBodiedOperators;
  130.     LispChar iPrevLastChar;
  131.     LispEnvironment* iCurrentEnvironment;
  132. };
  133.  
  134.  
  135. #endif
  136.  
  137.